package com.team7.happycommunity.propertymanagement.controller;

import com.team7.happycommunity.propertymanagement.common.CommonResult;
import com.team7.happycommunity.propertymanagement.utils.QiniuUploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/file")
@Controller
public class FileController {


    @PostMapping("/upload")
    @ResponseBody
    public CommonResult upload(MultipartFile file) {
        //获取文件名(自己编写)
        String fileName = UUID.randomUUID().toString();
        try {
            //返回的路径，即可存入数据库，即可以在网上直接访问（new       QiniuUploadUtil().upload）调取Util里面方法；
            //QiniuUploadUtil工具类不需要做任何修改
            String imgurl = new QiniuUploadUtil().upload(fileName, file.getBytes());
            Map map=new HashMap();
            map.put("src",imgurl);
            return CommonResult.success("上传成功",map);
        } catch (IOException e) {
            e.printStackTrace();
            return CommonResult.success("上传失败");
        }
    }
}
